package org.jeecg.glr.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.constant.CommonConstant;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.glr.entity.GlrLed;
import org.jeecg.glr.entity.GlrLedMember;
import org.jeecg.glr.entity.GlrResource;
import org.jeecg.glr.service.IGlrLedMemberService;
import org.jeecg.glr.service.IGlrLedService;
import org.jeecg.glr.service.IGlrResourceService;
import org.jeecg.glr.util.GLRConstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

/**
 * @Description: LED屏资源表
 * @Author: jeecg-boot
 * @Date:   2020-02-25
 * @Version: V1.0
 */
@RestController
@RequestMapping("/glr/glrLedMember")
@Slf4j
public class GlrLedMemberController extends JeecgController<GlrLedMember, IGlrLedMemberService> {
	@Autowired
	private IGlrLedMemberService glrLedMenberService;
	 @Autowired
	 private IGlrResourceService glrResourceService;
	@Autowired
	private IGlrLedService glrLedService;
	/**
	 * 分页列表查询
	 *
	 * @param glrLedMember
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@GetMapping(value = "/list")
	public Result<?> queryPageList(GlrLedMember glrLedMember,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<GlrLedMember> queryWrapper = QueryGenerator.initQueryWrapperCode(glrLedMember, req.getParameterMap());
		Page<GlrLedMember> page = new Page<GlrLedMember>(pageNo, pageSize);
		IPage<GlrLedMember> pageList = glrLedMenberService.page(page, queryWrapper);
		return Result.ok(pageList);
	}
	
	/**
	 *   添加
	 *
	 * @param glrLedMember
	 * @return
	 */
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody GlrLedMember glrLedMember) {
		glrLedMenberService.save(glrLedMember);
		return Result.ok("添加成功！");
	}
	
	/**
	 *  编辑
	 *
	 * @param glrLedMember
	 * @return
	 */
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody GlrLedMember glrLedMember) {
		glrLedMenberService.updateById(glrLedMember);
		return Result.ok("编辑成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		glrLedMenberService.removeById(id);
		return Result.ok("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.glrLedMenberService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.ok("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		GlrLedMember glrLedMember = glrLedMenberService.getById(id);
		if(glrLedMember ==null) {
			return Result.error("未找到对应数据");
		}
		return Result.ok(glrLedMember);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param glrLedMember
    */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, GlrLedMember glrLedMember) {
        return super.exportXls(request, glrLedMember, GlrLedMember.class, "LED屏资源表");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, GlrLedMember.class);
    }

	 /**
	  * 分页列表查询
	  *
	  * @param pageNo
	  * @param pageSize
	  * @param req
	  * @return
	  */
	 @GetMapping(value = "/ledResourceList")
	 public Result<?> ledResourceList(GlrResource glrResource,
									   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
									   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
									   HttpServletRequest req) {
		 QueryWrapper<GlrResource> queryWrapper = QueryGenerator.initQueryWrapperCode(glrResource, req.getParameterMap());
		 List<String> strList = glrLedMenberService.getResourceByColumnId(req.getParameter("columnId"));
		 if(strList!=null&&strList.size()>0){
			 queryWrapper.notIn("id", strList);
		 }
		 queryWrapper.eq("equipment_Type", GLRConstant.GLR_RESOURCE_TYPE_2);
		 Page<GlrResource> page = new Page<GlrResource>(pageNo, pageSize);
		 IPage<GlrResource> pageList = glrResourceService.page(page, queryWrapper);
		 return Result.ok(pageList);
	 }

	@GetMapping(value = "/resourceList")
	public Result<?> resourceList(GlrLedMember glrLedMember,
								  @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
								  @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
								  HttpServletRequest req) {
		QueryWrapper<GlrLedMember> queryWrapper = QueryGenerator.initQueryWrapperCode(glrLedMember, req.getParameterMap());
		Page<GlrLedMember> page = new Page<GlrLedMember>(pageNo, pageSize);
		IPage<GlrLedMember> pageList = glrLedMenberService.page(page, queryWrapper);
		List<GlrLedMember> list = pageList.getRecords();
		for (GlrLedMember m : list) {
			m.setGlrResource(glrResourceService.getById(m.getMemberId()));
		}
		return Result.ok(pageList);
	}

	/**
	 * 通过id查询
	 *
	 * @return
	 */
	@AutoLog(value = "LED屏内容绑定资源",logType= CommonConstant.LOG_TYPE_2)
	@PostMapping(value = "/bingResource")
	public Result<?> bingResource(@RequestBody GlrLedMember glrLedMember) {
		glrLedMenberService.bingResource(glrLedMember);
		return Result.ok();
	}
	/**
	 * 通过id查询
	 *
	 * @return
	 */
	@AutoLog(value = "LED屏内容绑定设备",logType= CommonConstant.LOG_TYPE_2)
	@PostMapping(value = "/bingLed")
	public Result<?> bingRadio(@RequestBody GlrLedMember glrLedMember) {
		glrLedMenberService.bingLed(glrLedMember);
		return Result.ok();
	}
	/**
	 * 分页列表查询
	 *
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@GetMapping(value = "/ledList")
	public Result<?> ledList(GlrLedMember glrLedMember,
							   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
							   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
							   HttpServletRequest req) {
		QueryWrapper<GlrLedMember> queryWrapper = QueryGenerator.initQueryWrapperCode(glrLedMember, req.getParameterMap());
		Page<GlrLedMember> page = new Page<GlrLedMember>(pageNo, pageSize);
		IPage<GlrLedMember> pageList = glrLedMenberService.page(page, queryWrapper);
		List<GlrLedMember> list = pageList.getRecords();
		for (GlrLedMember m : list) {
			m.setGlrLed(glrLedService.getById(m.getMemberId()));
		}
		return Result.ok(pageList);
	}

	/**
	 * 分页列表查询
	 *
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@GetMapping(value = "/columnLedlist")
	public Result<?> columnLedlist(GlrLed glrLed,
								   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
								   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<GlrLed> queryWrapper = QueryGenerator.initQueryWrapperCode(glrLed, req.getParameterMap());
		List<String> strList = glrLedMenberService.getLedByColumnId(req.getParameter("columnId"));
		if(strList!=null&&strList.size()>0){
			queryWrapper.notIn("id", strList);
		}
		Page<GlrLed> page = new Page<GlrLed>(pageNo, pageSize);
		IPage<GlrLed> pageList = glrLedService.page(page, queryWrapper);
		return Result.ok(pageList);
	}
}
